<template>
  <div class="user-box">
    <!-- 搜索 -->
    <el-form ref="form" :model="form" label-width="80px" style="display:flex">
      <el-form-item>
        <el-input v-model="form.searchipt" placeholder="请输入问题" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <div class="search">
          <el-button type="primary" @click="handleSearch" icon="el-icon-search">查找</el-button>
          <el-button type="primary" @click="handAdd" icon="el-icon-edit">添加</el-button>
          <el-button type="primary" @click="handchu" icon="el-icon-download">导出</el-button>
        </div>
      </el-form-item>
    </el-form>

    <!-- 表单 -->
    <div class="ipt">
      <el-table :data="tableData" border style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="问题ID" sortable width="150">
        </el-table-column>
        <el-table-column prop="question" label="问题内容" width="300">
        </el-table-column>
        <el-table-column prop="answer" label="问题回复" width="554">
        </el-table-column>
        <el-table-column fixed="right" label="操作" min-width="250">
          <template slot-scope="scope">
            <el-button @click="handeEdite(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="handleDel(scope.row)" type="text" size="small"
              style="color: #fff;background-color: #ff4949;">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="page">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :data=total
        :current-page="currentPage" :page-sizes="[10, 20, 30, 40, 50, 100]" :page-size="20"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 模态框 -->
    <el-dialog title="用户编辑" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
      <div class="conter">
        <el-form :model="ListQuery" ref="ListQuery" label-width="100px" class="demo-ListQuery" :data="ListQuery">
          <el-form-item label="问题" prop="question">
            <el-input v-model="ListQuery.question"></el-input>
          </el-form-item>
          <el-form-item label="回复" prop="answer">
            <el-input type="textarea" v-model="ListQuery.answer" style="width: 300px;"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm1('ListQuery')">确 定</el-button>
      </span>
    </el-dialog>
    <!--添加模态框 -->
    <el-dialog title="创建" :visible.sync="show" width="50%" :before-close="handleClose">
      <div class="conter">
        <el-form :model="Create" :rules="rules" ref="Create" label-width="100px" class="demo-ListQuery" :data="Create">
          <el-form-item label="问题" prop="question">
            <el-input v-model="Create.question"></el-input>
          </el-form-item>
          <el-form-item label="回复" prop="answer">
            <el-input type="textarea" v-model="Create.answer" style="width: 300px;"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('Create')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
import { listIssue, createIssue, deleteIssue, updateIssue } from '@/api/issue'
import * as XLSX from "xlsx";
export default {
  name: 'updateUser',
  data() {
    return {
      loading: false,
      dialogVisible: false,
      show: false,
      total: 0,
      currentPage: 1,
      searchipt1: '',
      searchipt4: '',
      searchipt3: '',
      tableData: [],
      form: {
        searchipt: ''
      },
      Create: {
        question: '',
        answer: ''
      },
      ListQuery: {
        page: 1,
        limit: 20,
        sort: 'add_time',
        order: 'desc',
      },
      rules: {
        question: [{ required: true, message: '关键词不能为空', trigger: 'blur' },],
        answer: [{ required: true, message: '回复不能为空', trigger: 'blur' }]
      }
    }
  },
  // 生命周期
  mounted() {
    this.handleGetList()
  },
  methods: {
    handchu() {
      // 点击导出的时候把现有数据生成一个新的数组 以中文作为key然后在导出
      let demo = [];
      for (let i = 0; i < this.tableData.length; i++) {
        let obje = {
          问题ID: this.tableData[i].id,
          问题内容: this.tableData[i].question,
          问题回复: this.tableData[i].answer,
        };
        demo.push(obje);
      }
      console.log(demo);
      const worksheet = XLSX.utils.json_to_sheet(demo); //这里面传入要导出的表格数据
      const workbook = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(workbook, worksheet, "data"); //data为自定义表格名字
      XLSX.writeFile(workbook, "用户信息.xlsx"); //one.xlsx指的是文件名
    },
    // 详情
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    // 分页器
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.ListQuery.limit = val
      this.handleGetList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.ListQuery.page = val
      this.handleGetList()
    },
    // 搜索姓名-用户id-手机号
    handleSearch() {
      this.loading = true;
      listIssue({
        page: this.ListQuery.page,
        limit: this.ListQuery.limit,
        question: this.form.searchipt
      }).then((res) => {
        this.tableData = res.data.list;
        this.total = res.data.total;
        this.loading = false;
      })
    },
    // 获取列表
    handleGetList() {
      this.loading = true;
      listIssue({
        page: this.ListQuery.page,
        limit: this.ListQuery.limit,
        question: this.searchipt1
      }).then((res) => {
        this.tableData = res.data.list;
        this.total = res.data.total;
        this.loading = false;
      })
    },
    // 添加
    handAdd() {
      this.show = true
    },
    // 编辑显示模态框
    handeEdite(row) {
      // 深拷贝
      this.ListQuery = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    // 确定编辑
    submitForm1(formName) {
      this.dialogVisible = false
      this.show = false
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 编辑
          updateIssue(this.ListQuery).then(res => {
            if (!res) { return }
            this.$message({
              message: '这是一条成功提示的信息',
              type: 'success'
            });
            this.loading = true;
            listIssue({
              page: 1,
              limit: 20,
            }).then((res) => {
              this.tableData = res.data.list;
              this.total = res.data.total;
              this.loading = false;
            })
          })
        } else {
          return false;
        }
      });
    },
    // 添加
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 编辑
          createIssue(this.Create).then(res => {
            if (!res) { return }
            this.$message({
              message: '这是一条成功提示的信息',
              type: 'success'
            });
            this.show = false
            this.handleGetList()
          })
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    // 删除
    handleDel(row) {
      this.$confirm('此操作将删除该留言, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteIssue({ ...row }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          // 删除成功后 调列表接口 重新渲染数据
          this.handleGetList()
        })
      }).catch(() => {
        this.$message({
          type: 'error',
          message: '删除失败'
        });
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep.user-box {

  .el-table td,
  .el-table th {
    padding: 6px 0 !important;
  }

  padding: 20px;

  .search {
    display: flex;
    margin-bottom: 20px;
    background-color: re;

    .el-input {
      width: 200px;
    }
  }

  .el-textarea__inner {
    min-height: 201.3333px !important;
  }

  .el-table .cell {
    text-align: center;

  }

  .cell {
    img {
      width: 50px;
      border-radius: 25px;
    }

    .yanse {
      background-color: #e8f4ff;
      border-color: #d1e9ff;
      color: #1890ff;
      padding: 5px;
      border-radius: 4px;
      font-size: 12px;
    }

  }

  .conter {
    padding: 30px 20px;
  }

  .el-dialog {

    .el-input {
      width: 50%;
    }

    .el-form-item__label {
      text-align: left;
    }
  }

  .el-input--suffix .el-input__inner {
    margin-right: 200px;
  }

  .el-button--text {
    color: #fff;
    display: inline-block;
    padding: 8px 13px;
    background-color: #1890ff;
  }

  .page {
    margin-top: 50px;
  }

  .el-table__body,
  .el-table__header {
    width: 100% !important;

  }


}
</style>s